package com.xiehua.task.job;

import com.xiehua.task.job.bucket.TopicBucket;
import com.xiehua.task.job.pool.Job;
import redis.clients.jedis.Jedis;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.xiehua.task.job.pool.Job.JobStatus.delay;

public abstract class AbsJobOperation<T extends Job> extends RidesOperationDefualt implements MachiningCenter {

    public AbsJobOperation(Jedis jedis) {
        super(jedis);
    }

    /***
     * 添加延迟任务
     * return jobId
     * **/
    public abstract T addJob(T job);

    /***
     * 更新job
     * **/
    public abstract void updateJob(T job);

    /***
     * 查询job
     * **/
    public abstract T queryJob(String jobId);

    /***
     * 添加job
     * @param job
     */
    @Override
    public void machiningJob(Job job) {
        Long now = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
        Double score = new BigDecimal(now).add(new BigDecimal(job.getDelay())).doubleValue();
        jedis.zadd(JOB_BUCKET_PIX + job.getTopic(), score, job.getJobId());
    }


}
